
import mixin from '@/service/mixin';
import Pagination from '@/components/pagination';
import { keyMap } from '@/config';
import {config} from "ace-builds";
import instance from "@/libs/http";

export default {
    name: 'coupon-manual',
    components: {
        Pagination,
    },
    mixins: [mixin],
    data() {
        return {
            keyMap,
            user: {},
            form: {
                discountCouponType: '',
                appName: '',
                dateRange: [],
            },
            rules: {
            },
            appList: [],
        };
    },
    methods: {
        // 获取列表数据
        getTableData(page = this.page) {
            const params = this.fillParams(page);
            this.$api.operation.getTicketStatisticsInfo(params).then(res => {
                const {status, data, error} = res;
                if ('1' === status && '00000000' === error && data) {
                    this.tableData = data.resultData.map(item => {
                        item.usedData = [];
                        item.isLoadingUsedData = false;
                        return item;
                    });
                    this.setPage(data.page, this.page);
                }
                this.tableMaxHeight = document.body.clientHeight - 240;
            });
        },
        // 搜索列表数据
        search() {
            const { discountCouponType, appName, dateRange } = this.form;
            if (!discountCouponType && !appName && dateRange.length === 0) {
                this.$message.warning(this.$t('verify.plsCheckFilters'));
                return;
            }
            this.$refs.form.validate(val => {
                if (val) {
                    this.page.currentPage = 1;
                    this.getTableData();
                }
            });
        },
        // 配置查询和导出参数
        fillParams(page = this.page) {
            const { discountCouponType, appName, dateRange } = this.form;
            const params = {
                discountCouponType,
                appName,
                dateBeginL: '',
                dateEndL: '',
                page: {
                    pageNo: page.currentPage,
                    pageSize: this.page.pageSize
                },
            };
            if (dateRange && dateRange.length) {
                params.dateBeginL = dateRange[0];
                params.dateEndL = dateRange[1];
            }
            return params;
        },
        // 获取APP列表
        getAppList() {
            const params = {
                appListDomain: this.user.domain
            };
            this.$api.systemManage.getAppListdomain(params).then(res => {
                const { status, data, error } = res;
                if ('1' === status && error === '00000000' && data instanceof Array) {
                    this.appList = data;
                }
            });
        },
        // 导出数据
        exportExcel() {
            // exportTicketStatisticsExcel
            const params = this.fillParams();
            delete params.page;
            const user = this.$storage.get('user');
            const language = this.$storage.getLocal('language');
            params.language = language;
            params.token = user.token;
            const {$apiConf} = this;
            const url = this.$urlUtil.fillParams(params, $apiConf.baseUrl + $apiConf.urlType.operation.exportTicketStatisticsExcel, ['page']);
            this.$openNewBlank(url);
            // const config = {
            //     responseType: 'blob',
            //     keepConfig: true,
            // };
            // this.$api.operation.exportTicketStatisticsExcel(params, config).then(res => {
            //     if (res.status === 200) {
            //         const link = document.createElement('a');
            //         link.style.display = 'none';
            //         const formatDate = this.$options.filters.formatDate;
            //         link.download = `ticket_statistics_info_${formatDate(new Date(), 'yyyy_MM_dd')}.xls`;
            //         link.href = URL.createObjectURL(res.request.response);
            //         document.body.appendChild(link);
            //         link.click();
            //         document.body.removeChild(link);
            //     } else {
            //         this.$message.error('Request error, Please try again later');
            //     }
            // });
        },
        // 查询试用详情
        showCouponDetail(row) {
            if (row.usedData.length) return;
            const d = new Date(row.dateDay);
            d.setHours(0);
            d.setMinutes(0);
            d.setSeconds(0);
            d.setMilliseconds(0);
            const params = {
                dateBeginL: d.getTime(),
                appName: row.appName,
                discountCouponType: row.type,
            };
            row.isLoadingUsedData = true;
            this.$api.operation.getUsedTicketStatistics(params).then(res => {
                const { status, data }  = res;
                if ('1' === status) {
                    row.usedData = data;
                }
                row.isLoadingUsedData = false;
            });
        },
    },
    filters: {
    },
    created() {
        this.user = this.$storage.get('user');
        // this.getTableData({currentPage: this.page.currentPage}, this.form);
        this.getAppList();
    },
}
